<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge" >
  <title>Javascript 严格模式 | 前端.林姐姐</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="除了正常运行模式，ECMAscript 5添加了第二种运行模式：”严格模式“（strict mode）。顾名思义，这种模式使得Javascript在更严格的条件下运行。
设立”严格模式”的目的:1234- 消除Javascript语法的一些不合理、不严谨之处，减少一些怪异行为;　　- 消除代码运行的一些不安全之处，保证代码运行的安全；　　- 提高编译器效率，增加运行速度；　　- 为未来新版本的Ja">
<meta property="og:type" content="article">
<meta property="og:title" content="Javascript 严格模式">
<meta property="og:url" content="http://yoursite.com/2016/02/01/Javascript-严格模式/index.html">
<meta property="og:site_name" content="前端.林姐姐">
<meta property="og:description" content="除了正常运行模式，ECMAscript 5添加了第二种运行模式：”严格模式“（strict mode）。顾名思义，这种模式使得Javascript在更严格的条件下运行。
设立”严格模式”的目的:1234- 消除Javascript语法的一些不合理、不严谨之处，减少一些怪异行为;　　- 消除代码运行的一些不安全之处，保证代码运行的安全；　　- 提高编译器效率，增加运行速度；　　- 为未来新版本的Ja">
<meta property="og:updated_time" content="2016-02-01T06:55:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Javascript 严格模式">
<meta name="twitter:description" content="除了正常运行模式，ECMAscript 5添加了第二种运行模式：”严格模式“（strict mode）。顾名思义，这种模式使得Javascript在更严格的条件下运行。
设立”严格模式”的目的:1234- 消除Javascript语法的一些不合理、不严谨之处，减少一些怪异行为;　　- 消除代码运行的一些不安全之处，保证代码运行的安全；　　- 提高编译器效率，增加运行速度；　　- 为未来新版本的Ja">
  
    <link rel="alternative" href="/atom.xml" title="前端.林姐姐" type="application/atom+xml">
  
  
    <link rel="icon" href="img/linjie.jpg">
  
  <link rel="stylesheet" href="/css/style.css" type="text/css">
</head>

<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="undefined" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">林姐姐</a></h1>
		</hgroup>

		
		<p class="header-subtitle">让未来的你的感谢现在的自己！</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
						<div class="icon-wrap icon-me hide" data-idx="3">
							<div class="user"></div>
							<div class="shoulder"></div>
						</div>
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>標籤</li>
						
						<li>友情链接</li>
						
						
						<li>關於</li>
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/">主页</a></li>
				        
							<li><a href="/archives">所有文章</a></li>
				        
							<li><a href="/categories/学习笔记/">学习笔记</a></li>
				        
							<li><a href="/categories/技术摘录/">技术摘录</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
								<a class="github" target="_blank" href="https://github.com/linjieFE" title="github">github</a>
					        
								<a class="mail" target="_blank" href="mailto:solocode@163.com" title="mail">mail</a>
					        
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						<a href="/tags/gulp/" style="font-size: 10px;">gulp</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/javascript/" style="font-size: 10px;">javascript</a> <a href="/tags/mac技巧/" style="font-size: 13.33px;">mac技巧</a> <a href="/tags/react/" style="font-size: 10px;">react</a> <a href="/tags/react-native/" style="font-size: 10px;">react-native</a> <a href="/tags/vim/" style="font-size: 10px;">vim</a> <a href="/tags/前题面试题/" style="font-size: 10px;">前题面试题</a> <a href="/tags/学习笔记/" style="font-size: 20px;">学习笔记</a> <a href="/tags/终端/" style="font-size: 10px;">终端</a> <a href="/tags/随笔/" style="font-size: 16.67px;">随笔</a>
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.ruanyifeng.com/home.html">阮一峰ES6</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://nodeapi.ucdok.com/api/">NodeJS在线文档</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://jquery.cuishifeng.cn">jquery在线文档</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.w3school.com.cn/js/">javascript手册</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.phpstudy.net/css3/">css3在线手册</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://blog.css6.com">凯哥的博客</a>
			        
			        </div>
				</section>
				

				
				
				<section class="switch-part switch-part4">
				
					<div id="js-aboutme">80s，来自祖国最东端乌苏里江畔。爱好运动和音乐，希望在这里可以跟一样爱好前端的朋友共同进步！</div>
				</section>
				
			</div>
		</div>
	</header>				
</div>

    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">林姐姐</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
			
				<img lazy-src="undefined" class="js-avatar">
			
			</div>
			<hgroup>
			  <h1 class="header-author">林姐姐</h1>
			</hgroup>
			
			<p class="header-subtitle">让未来的你的感谢现在的自己！</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/">主页</a></li>
		        
					<li><a href="/archives">所有文章</a></li>
		        
					<li><a href="/categories/学习笔记/">学习笔记</a></li>
		        
					<li><a href="/categories/技术摘录/">技术摘录</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
						<a class="github" target="_blank" href="https://github.com/linjieFE" title="github">github</a>
			        
						<a class="mail" target="_blank" href="mailto:solocode@163.com" title="mail">mail</a>
			        
				</div>
			</nav>
		</header>				
	</div>
</nav>

      <div class="body-wrap"><article id="post-Javascript-严格模式" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2016/02/01/Javascript-严格模式/" class="article-date">
  	<time datetime="2016-02-01T03:08:22.000Z" itemprop="datePublished">2016-02-01</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Javascript 严格模式
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
	<div class="article-tag tagcloud">
		<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/javascript/">javascript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/学习笔记/">学习笔记</a></li></ul>
	</div>

        
	<div class="article-category tagcloud">
	<a class="article-category-link" href="/categories/学习笔记/">学习笔记</a>
	</div>


        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>除了正常运行模式，ECMAscript 5添加了第二种运行模式：”<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode" target="_blank" rel="external">严格模式</a>“（strict mode）。顾名思义，这种模式使得Javascript在更严格的条件下运行。</p>
<h2 id="u8BBE_u7ACB_u201D_u4E25_u683C_u6A21_u5F0F_u201D_u7684_u76EE_u7684_3A"><a href="#u8BBE_u7ACB_u201D_u4E25_u683C_u6A21_u5F0F_u201D_u7684_u76EE_u7684_3A" class="headerlink" title="设立”严格模式”的目的:"></a>设立”严格模式”的目的:</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- 消除Javascript语法的一些不合理、不严谨之处，减少一些怪异行为;</span><br><span class="line">　　- 消除代码运行的一些不安全之处，保证代码运行的安全；</span><br><span class="line">　　- 提高编译器效率，增加运行速度；</span><br><span class="line">　　- 为未来新版本的Javascript做好铺垫。</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>“严格模式”体现了Javascript更合理、更安全、更严谨的发展方向，包括IE 10在内的主流浏览器，都已经支持它，许多大项目已经开始全面拥抱它。<br>另一方面，同样的代码，在”严格模式”中，可能会有不一样的运行结果；一些在”正常模式”下可以运行的语句，在”严格模式”下将不能运行。掌握这些内容，有助于更细致深入地理解Javascript，让你变成一个更好的程序员。<br>本文将对”严格模式”做详细介绍。</p>
<h2 id="u4E8C_u3001_u8FDB_u5165_u6807_u5FD7"><a href="#u4E8C_u3001_u8FDB_u5165_u6807_u5FD7" class="headerlink" title="二、进入标志"></a>二、进入标志</h2><p>进入”严格模式”的标志，是下面这行语句：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br></pre></td></tr></table></figure></p>
<p>老版本的浏览器会把它当作一行普通字符串，加以忽略。</p>
<h2 id="u4E09_u3001_u5982_u4F55_u8C03_u7528"><a href="#u4E09_u3001_u5982_u4F55_u8C03_u7528" class="headerlink" title="三、如何调用"></a>三、如何调用</h2><p>“严格模式”有两种调用方法，适用于不同的场合。</p>
<h3 id="3-1__u9488_u5BF9_u6574_u4E2A_u811A_u672C_u6587_u4EF6"><a href="#3-1__u9488_u5BF9_u6574_u4E2A_u811A_u672C_u6587_u4EF6" class="headerlink" title="3.1 针对整个脚本文件"></a>3.1 针对整个脚本文件</h3><p>将”use strict”放在脚本文件的第一行，则整个脚本都将以”严格模式”运行。如果这行语句不在第一行，则无效，整个脚本以”正常模式”运行。如果不同模式的代码文件合并成一个文件，这一点需要特别注意。<br>(严格地说，只要前面不是产生实际运行结果的语句，”use strict”可以不在第一行，比如直接跟在一个空的分号后面。)<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">　　&lt;script&gt;</span><br><span class="line">　　　　<span class="string">"use strict"</span>;</span><br><span class="line">　　　　<span class="built_in">console</span>.log(<span class="string">"这是严格模式。"</span>);</span><br><span class="line">　　<span class="xml"><span class="tag">&lt;/<span class="title">script</span>&gt;</span></span><br><span class="line">　　<span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span><br><span class="line">　　　　<span class="built_in">console</span>.log(<span class="string">"这是正常模式。"</span>);</span><br><span class="line">　　</span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure></p>
<p>上面的代码表示，一个网页中依次有两段Javascript代码。前一个script标签是严格模式，后一个不是。</p>
<h3 id="3-2__u9488_u5BF9_u5355_u4E2A_u51FD_u6570"><a href="#3-2__u9488_u5BF9_u5355_u4E2A_u51FD_u6570" class="headerlink" title="3.2 针对单个函数"></a>3.2 针对单个函数</h3><p>将”use strict”放在函数体的第一行，则整个函数以”严格模式”运行。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">strict</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　<span class="keyword">return</span> <span class="string">"这是严格模式。"</span>;</span><br><span class="line">　　&#125;</span><br><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">notStrict</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">　　　　<span class="keyword">return</span> <span class="string">"这是正常模式。"</span>;</span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="3-3__u811A_u672C_u6587_u4EF6_u7684_u53D8_u901A_u5199_u6CD5"><a href="#3-3__u811A_u672C_u6587_u4EF6_u7684_u53D8_u901A_u5199_u6CD5" class="headerlink" title="3.3 脚本文件的变通写法"></a>3.3 脚本文件的变通写法</h3><p>因为第一种调用方法不利于文件合并，所以更好的做法是，借用第二种方法，将整个脚本文件放在一个立即执行的匿名函数之中。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">　　(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="pi"></span><br><span class="line">　　　　"use strict"</span>;</span><br><span class="line">　　　　<span class="comment">// some code here</span></span><br><span class="line"></span><br><span class="line">　　 &#125;)();</span><br></pre></td></tr></table></figure></p>
<h2 id="u56DB_u3001_u8BED_u6CD5_u548C_u884C_u4E3A_u6539_u53D8"><a href="#u56DB_u3001_u8BED_u6CD5_u548C_u884C_u4E3A_u6539_u53D8" class="headerlink" title="四、语法和行为改变"></a>四、语法和行为改变</h2><p>严格模式对Javascript的语法和行为，都做了一些改变。</p>
<h3 id="4-1__u5168_u5C40_u53D8_u91CF_u663E_u5F0F_u58F0_u660E"><a href="#4-1__u5168_u5C40_u53D8_u91CF_u663E_u5F0F_u58F0_u660E" class="headerlink" title="4.1 全局变量显式声明"></a>4.1 全局变量显式声明</h3><p>在正常模式中，如果一个变量没有声明就赋值，默认是全局变量。严格模式禁止这种用法，全局变量必须显式声明。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">"use strict"</span>;</span><br><span class="line">　　v = <span class="number">1</span>; <span class="comment">// 报错，v未声明</span></span><br><span class="line">　　<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++) &#123; <span class="comment">// 报错，i未声明</span></span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<p>因此，严格模式下，变量都必须先用var命令声明，然后再使用。</p>
<h3 id="4-2__u9759_u6001_u7ED1_u5B9A"><a href="#4-2__u9759_u6001_u7ED1_u5B9A" class="headerlink" title="4.2 静态绑定"></a>4.2 静态绑定</h3><p>Javascript语言的一个特点，就是允许”动态绑定”，即某些属性和方法到底属于哪一个对象，不是在编译时确定的，而是在运行时（runtime）确定的。<br>严格模式对动态绑定做了一些限制。某些情况下，只允许静态绑定。也就是说，属性和方法到底归属哪个对象，在编译阶段就确定。这样做有利于编译效率的提高，也使得代码更容易阅读，更少出现意外。<br>具体来说，涉及以下几个方面。<br>（1）禁止使用with语句<br>因为with语句无法在编译时就确定，属性到底归属哪个对象。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> v = <span class="number">1</span>;</span><br><span class="line">　　<span class="keyword">with</span> (o)&#123; <span class="comment">// 语法错误</span></span><br><span class="line">　　　　v = <span class="number">2</span>;</span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<p>（2）创设eval作用域<br>正常模式下，Javascript语言有两种变量作用域（scope）：全局作用域和函数作用域。严格模式创设了第三种作用域：eval作用域。<br>正常模式下，eval语句的作用域，取决于它处于全局作用域，还是处于函数作用域。严格模式下，eval语句本身就是一个作用域，不再能够生成全局变量了，它所生成的变量只能用于eval内部。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> x = <span class="number">2</span>;</span><br><span class="line">　　<span class="built_in">console</span>.info(<span class="built_in">eval</span>(<span class="string">"var x = 5; x"</span>)); <span class="comment">// 5</span></span><br><span class="line">　　<span class="built_in">console</span>.info(x); <span class="comment">// 2</span></span><br></pre></td></tr></table></figure></p>
<h3 id="4-3__u589E_u5F3A_u7684_u5B89_u5168_u63AA_u65BD"><a href="#4-3__u589E_u5F3A_u7684_u5B89_u5168_u63AA_u65BD" class="headerlink" title="4.3 增强的安全措施"></a>4.3 增强的安全措施</h3><p>（1）禁止this关键字指向全局对象<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">　　　　<span class="keyword">return</span> !<span class="keyword">this</span>;</span><br><span class="line">　　&#125;</span><br><span class="line">　　<span class="comment">// 返回false，因为"this"指向全局对象，"!this"就是false</span></span><br><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　<span class="keyword">return</span> !<span class="keyword">this</span>;</span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　// 返回true，因为严格模式下，this的值为undefined，所以”!this”为true。<br>因此，使用构造函数时，如果忘了加new，this不再指向全局对象，而是报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　<span class="keyword">this</span>.a = <span class="number">1</span>;</span><br><span class="line">　　&#125;;</span><br><span class="line">　　f();<span class="comment">// 报错，this未定义</span></span><br></pre></td></tr></table></figure></p>
<p>（2）禁止在函数内部遍历调用栈<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　f1.caller; <span class="comment">// 报错</span></span><br><span class="line">　　　　f1.arguments; <span class="comment">// 报错</span></span><br><span class="line">　　&#125;</span><br><span class="line">　　f1();</span><br></pre></td></tr></table></figure></p>
<h3 id="4-4__u7981_u6B62_u5220_u9664_u53D8_u91CF"><a href="#4-4__u7981_u6B62_u5220_u9664_u53D8_u91CF" class="headerlink" title="4.4 禁止删除变量"></a>4.4 禁止删除变量</h3><p>严格模式下无法删除变量。只有configurable设置为true的对象属性，才能被删除。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> x;</span><br><span class="line">　　<span class="keyword">delete</span> x; <span class="comment">// 语法错误</span></span><br><span class="line">　　<span class="keyword">var</span> o = <span class="built_in">Object</span>.create(<span class="literal">null</span>, &#123;<span class="string">'x'</span>: &#123;</span><br><span class="line">　　　　　　value: <span class="number">1</span>,</span><br><span class="line">　　　　　　configurable: <span class="literal">true</span></span><br><span class="line">　　&#125;&#125;);</span><br><span class="line">　　<span class="keyword">delete</span> o.x; <span class="comment">// 删除成功</span></span><br></pre></td></tr></table></figure></p>
<h2 id="4-5__u663E_u5F0F_u62A5_u9519"><a href="#4-5__u663E_u5F0F_u62A5_u9519" class="headerlink" title="4.5 显式报错"></a>4.5 显式报错</h2><p>正常模式下，对一个对象的只读属性进行赋值，不会报错，只会默默地失败。严格模式下，将报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line"></span><br><span class="line">　　<span class="keyword">var</span> o = &#123;&#125;;</span><br><span class="line">　　<span class="built_in">Object</span>.defineProperty(o, <span class="string">"v"</span>, &#123; value: <span class="number">1</span>, writable: <span class="literal">false</span> &#125;);</span><br><span class="line">　　o.v = <span class="number">2</span>; <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></p>
<p>严格模式下，对一个使用getter方法读取的属性进行赋值，会报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line"></span><br><span class="line">　　<span class="keyword">var</span> o = &#123;</span><br><span class="line"></span><br><span class="line">　　　　get v() &#123; <span class="keyword">return</span> <span class="number">1</span>; &#125;</span><br><span class="line">　　&#125;;</span><br><span class="line">　　o.v = <span class="number">2</span>; <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></p>
<p>严格模式下，对禁止扩展的对象添加新属性，会报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> o = &#123;&#125;;</span><br><span class="line">　　<span class="built_in">Object</span>.preventExtensions(o);</span><br><span class="line">　　o.v = <span class="number">1</span>; <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></p>
<p>严格模式下，删除一个不可删除的属性，会报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">delete</span> <span class="built_in">Object</span>.prototype; <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></p>
<h3 id="4-6__u91CD_u540D_u9519_u8BEF"><a href="#4-6__u91CD_u540D_u9519_u8BEF" class="headerlink" title="4.6 重名错误"></a>4.6 重名错误</h3><p>严格模式新增了一些语法错误。<br>（1）对象不能有重名的属性<br>正常模式下，如果对象有多个重名属性，最后赋值的那个属性会覆盖前面的值。严格模式下，这属于语法错误。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> o = &#123;</span><br><span class="line">　　　　p: <span class="number">1</span>,</span><br><span class="line">　　　　p: <span class="number">2</span></span><br><span class="line">　　&#125;; <span class="comment">// 语法错误</span></span><br></pre></td></tr></table></figure></p>
<p>（2）函数不能有重名的参数<br>正常模式下，如果函数有多个重名的参数，可以用arguments[i]读取。严格模式下，这属于语法错误。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">a, a, b</span>) </span>&#123; <span class="comment">// 语法错误</span></span><br><span class="line">　　　　<span class="keyword">return</span> ;</span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="4-7__u7981_u6B62_u516B_u8FDB_u5236_u8868_u793A_u6CD5"><a href="#4-7__u7981_u6B62_u516B_u8FDB_u5236_u8868_u793A_u6CD5" class="headerlink" title="4.7 禁止八进制表示法"></a>4.7 禁止八进制表示法</h3><p>正常模式下，整数的第一位如果是0，表示这是八进制数，比如0100等于十进制的64。严格模式禁止这种表示法，整数第一位为0，将报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> n = <span class="number">0100</span>; <span class="comment">// 语法错误</span></span><br></pre></td></tr></table></figure></p>
<h3 id="4-8_arguments_u5BF9_u8C61_u7684_u9650_u5236"><a href="#4-8_arguments_u5BF9_u8C61_u7684_u9650_u5236" class="headerlink" title="4.8 arguments对象的限制"></a>4.8 arguments对象的限制</h3><p>arguments是函数的参数对象，严格模式对它的使用做了限制。<br>（1）不允许对arguments赋值<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="built_in">arguments</span>++; <span class="comment">// 语法错误</span></span><br><span class="line">　　<span class="keyword">var</span> obj = &#123; set p(<span class="built_in">arguments</span>) &#123; &#125; &#125;; <span class="comment">// 语法错误</span></span><br><span class="line">　　<span class="keyword">try</span> &#123; &#125; <span class="keyword">catch</span> (<span class="built_in">arguments</span>) &#123; &#125; <span class="comment">// 语法错误</span></span><br><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">arguments</span>(<span class="params"></span>) </span>&#123; &#125; <span class="comment">// 语法错误</span></span><br><span class="line">　　<span class="keyword">var</span> f = <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">"arguments"</span>, <span class="string">"'use strict'; return 17;"</span>); <span class="comment">// 语法错误</span></span><br></pre></td></tr></table></figure></p>
<p>（2）arguments不再追踪参数的变化<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">a</span>) </span>&#123;</span><br><span class="line">　　　　a = <span class="number">2</span>;</span><br><span class="line">　　　　<span class="keyword">return</span> [a, <span class="built_in">arguments</span>[<span class="number">0</span>]];</span><br><span class="line">　　&#125;</span><br><span class="line">　　f(<span class="number">1</span>); <span class="comment">// 正常模式为[2,2]</span></span><br><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">a</span>) </span>&#123;</span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　a = <span class="number">2</span>;</span><br><span class="line">　　　　<span class="keyword">return</span> [a, <span class="built_in">arguments</span>[<span class="number">0</span>]];</span><br><span class="line">　　&#125;</span><br><span class="line">　　f(<span class="number">1</span>); <span class="comment">// 严格模式为[2,1]</span></span><br></pre></td></tr></table></figure></p>
<p>（3）禁止使用arguments.callee<br>这意味着，你无法在匿名函数内部调用自身了。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">var</span> f = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="built_in">arguments</span>.callee; &#125;;</span><br><span class="line">　　f(); <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></p>
<h3 id="4-9__u51FD_u6570_u5FC5_u987B_u58F0_u660E_u5728_u9876_u5C42"><a href="#4-9__u51FD_u6570_u5FC5_u987B_u58F0_u660E_u5728_u9876_u5C42" class="headerlink" title="4.9 函数必须声明在顶层"></a>4.9 函数必须声明在顶层</h3><p>将来Javascript的新版本会引入”块级作用域”。为了与新版本接轨，严格模式只允许在全局作用域或函数作用域的顶层声明函数。也就是说，不允许在非函数的代码块内声明函数。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="pi">　　"use strict"</span>;</span><br><span class="line">　　<span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">　　　　<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>) </span>&#123; &#125; <span class="comment">// 语法错误</span></span><br><span class="line">　　&#125;</span><br><span class="line">　　<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">　　　　<span class="function"><span class="keyword">function</span> <span class="title">f2</span>(<span class="params"></span>) </span>&#123; &#125; <span class="comment">// 语法错误</span></span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="4-10__u4FDD_u7559_u5B57"><a href="#4-10__u4FDD_u7559_u5B57" class="headerlink" title="4.10 保留字"></a>4.10 保留字</h3><p>为了向将来Javascript的新版本过渡，严格模式新增了一些保留字：implements, interface, let, package, private, protected, public, static, yield。<br>使用这些词作为变量名将会报错。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">　　<span class="function"><span class="keyword">function</span> <span class="title">package</span>(<span class="params">protected</span>) </span>&#123; <span class="comment">// 语法错误</span></span><br><span class="line"><span class="pi">　　　　"use strict"</span>;</span><br><span class="line">　　　　<span class="keyword">var</span> implements; <span class="comment">// 语法错误</span></span><br><span class="line">　　&#125;</span><br></pre></td></tr></table></figure></p>
<p>此外，ECMAscript第五版本身还规定了另一些保留字（class, enum, export, extends, import, super），以及各大浏览器自行增加的const保留字，也是不能作为变量名的。</p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/02/02/for循环的执行顺序/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption"><</strong>
      <div class="article-nav-title">
        
          for循环的执行顺序
        
      </div>
    </a>
  
  
    <a href="/2016/01/26/关于haslayout/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">关于IE的下的haslayout问题</div>
      <strong class="article-nav-caption">></strong>
    </a>
  
</nav>

  
</article>


<div class="share_jia">
	<!-- JiaThis Button BEGIN -->
	<div class="jiathis_style">
		<span class="jiathis_txt">分享到: &nbsp; </span>
		<a class="jiathis_button_facebook"></a> 
    <a class="jiathis_button_twitter"></a>
    <a class="jiathis_button_plus"></a> 
    <a class="jiathis_button_tsina"></a>
		<a class="jiathis_button_cqq"></a>
		<a class="jiathis_button_douban"></a>
		<a class="jiathis_button_weixin"></a>
		<a class="jiathis_button_tumblr"></a>
    <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	</div>
	<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=1405949716054953" charset="utf-8"></script>
	<!-- JiaThis Button END -->
</div>






<div class="duoshuo">
	<!-- 多说评论框 start -->
	<div class="ds-thread" data-thread-key="Javascript-严格模式" data-title="Javascript 严格模式" data-url="http://yoursite.com/2016/02/01/Javascript-严格模式/"></div>
	<!-- 多说评论框 end -->
	<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"true"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
	<!-- 多说公共JS代码 end -->
</div>




</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
    	<div class="footer-left">
    		&copy; 2016 林姐姐
    	</div>
      	<div class="footer-right">
      		<a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/litten/hexo-theme-yilia" target="_blank">Yilia</a> by Litten
      	</div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="/js/main.js" type="text/javascript"></script>






<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  </div>
</body>
</html>